<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>

<body>
    <!-- 给一个容器 -->
    <div class="container">
        <div class="row text-center">
            <h1>学生信息管理系统</h1>
            <hr>

            <a href="" class="btn btn-success">首页</a>
            <a href="" class="btn btn-warning">添加</a>

            <br>
            <br>
        </div>

        <!-- col-md-8 中等屏幕的8  12 -->
        <!-- <form action="/edit" method="post" class="col-md-5"> -->

            <div class="row">
                <div class="form-group col-md-5">
                    <label for="name">姓名</label>
                    <input type="text" name="name"  id="name" class="form-control">
                </div>
            </div>

            <div class="row">
                <div class="form-group col-md-5">
                    <label for="age">年龄</label>
                    <input type="text" name="age" id="age"  class="form-control">
                </div>
            </div>
            <div class="row">
                <div class="form-group col-md-5">
                    <label for="sex">性别</label>
                    <input type="text" name="sex" id="sex" class="form-control">
                </div>
            </div>


            <div class="row">
                <div class="form-group col-md-5">
                    <label for="claass">班级</label>
                    <input type="text" name="claass" id="claass" class="form-control">
                </div>
            </div>
            <div class="row">
                <div class="form-group col-md-5">
                    <label for="score">成绩</label>
                    <input type="text" name="score" id="score" class="form-control">
                </div>
            </div>
            <div class="row">
                <div class="text-center col-md-5">
                    <button type="submit" class="btn btn-success" id="btn">提交</button>
                    <button type="reset" class="btn btn-danger">重置</button>
                </div>
            </div>
    </div>
    <script src="./js/jquery.min.js"></script>
    <script>
        let id = location.hash.substring(1) ;
        $.ajax({
            url: 'http://127.0.0.1:3000/edit',
            type: 'post',
            data: {
                id: id
            },
            success: function (res) {
                console.log(res);
                $('#name').val(res.data[0].name);
                $('#age').val(res.data[0].age);
                $('#sex').val(res.data[0].sex);
                $('#claass').val(res.data[0].class);
                $('#score').val(res.data[0].score);

            }
        })
        $('#btn').on('click', function () {
            let name = $('#name').val();
            let age = $('#age').val();
            let sex = $('#sex').val();
            let claass = $('#claass').val();
            let score = $('#score').val();
            console.log(name, age, sex, claass, score);
            $.ajax({
                url: 'http://127.0.0.1:3000/add',
                type: 'post',
                data: {
                    name: name,
                    age: age,
                    sex: sex,
                    claass: claass,
                    score: score
                },
                success: function (res) {
                    location.href = './index.html'
                }
            })
        })
    </script>
</body>

</html>